package com.rfid.a252robot.admin;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.rfid.a252robot.R;
import com.rfid.a252robot.admin.bean.Address;
import com.rfid.a252robot.event.RobotTaskEvent;
import com.rfid.common.log.LogUtils;
import com.rfid.common.util.SharedPreferencesUtil;
import com.rfid.mqtt.MqttManager;
import com.rfid.mqtt.bean.TaskInfo;
import com.rfid.mqtt.bean.TaskInfoRespon;
import com.rfid.util.ACache;
import com.rfid.util.Util;
import com.spectre.generate.ConfigManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.OnClick;


public class AdminHomeActivity extends Activity {

    TextView mTaskView;
    CheckBox mTestMode;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_home);
        mTaskView = findViewById(R.id.task);
        mTestMode = findViewById(R.id.test_mode);
        boolean test = SharedPreferencesUtil.getInstance().getBoolean("test_mode");
        mTestMode.setChecked(test);
        mTestMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferencesUtil.getInstance().putBoolean("test_mode",isChecked);
            }
        });
        TaskInfo taskInfo = MqttManager.INSTANCE.getCurrentTask();
        if (taskInfo == null) {
            mTaskView.setText("当前无任务");
        } else {
            mTaskView.setText("当前任务状态：" + getTaskStr(taskInfo.getStatus()));
        }
        EventBus.getDefault().register(this);
        MqttManager.INSTANCE.queryStatus();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTaskEvent(RobotTaskEvent event) {
        LogUtils.v("onTaskEvent = " + event.getTaskInfo());
        TaskInfo taskInfo = event.getTaskInfo();
        if (taskInfo != null) {
            mTaskView.setText("当前任务状态：" + getTaskStr(taskInfo.getStatus()));
        }
    }

    private String getTaskStr(int status) {
        switch (status) {
            case -1:
                return "未执行";
            case 0:
                return "发起失败";
            case 1:
                return "进行中";
            case 2:
                return "异常挂起";
            case 3:
                return "异常结束";
            case 4:
                return "成功结束";
            case 5:
                return "取消结束";
            case 6:
                return "任务暂停";
        }
        return status + "";
    }

    public void selectRoute(View v) {
        Intent intent = new Intent();
        intent.setClass(this, RouteSelectActivity.class);
        startActivity(intent);
    }

    public void charge(View v) {
        String mapCode = ConfigManager.INSTANCE.getChargeConfig().getMapCode();
        String pointName = ConfigManager.INSTANCE.getChargeConfig().getPointName();
        if (TextUtils.isEmpty(mapCode) || TextUtils.isEmpty(pointName)) {
            Toast.makeText(this,"请先在参数配置里配置充电点位",Toast.LENGTH_SHORT).show();
            return;
        }
        MqttManager.INSTANCE.sendCharge(mapCode,pointName);
    }

    public void openBox(View v) {
        Util.startActivity(this,DoorBoxActivity.class);
    }

    public void config(View v) {
        Util.startActivity(this,ConfigActivity.class);
    }

    public void shelf(View v) {
        Intent intent = new Intent();
        intent.putExtra("shelf",true);
        intent.setClass(this, DoorBoxActivity.class);
        startActivity(intent);
    }

    public void setTime(View v) {
        Util.startActivity(this,TimeSetActivity.class);
    }

    public void startTask(View view) {
        List<Address> ss = ACache.get().getAsAddresses("selectRoutes");
        if (ss.size() == 0) {
            Toast.makeText(this,"请先编辑路径",Toast.LENGTH_SHORT).show();
            return;
        }
        String ps[] = new String[ss.size()];
        for (int i=0;i<ss.size();i++) {
            ps[i] = ss.get(i).getBizPointId();
        }
        MqttManager.INSTANCE.startMoveTask(true);
        Toast.makeText(AdminHomeActivity.this,"启动任务",Toast.LENGTH_SHORT).show();
/*        new AlertDialog.Builder(this)
                .setMessage("是否循环执行")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MqttManager.INSTANCE.startMoveTask(true);
                        Toast.makeText(AdminHomeActivity.this,"启动循环任务",Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MqttManager.INSTANCE.startMoveTask(false);
                        Toast.makeText(AdminHomeActivity.this,"启动单次任务",Toast.LENGTH_SHORT).show();
                    }
                }).show();*/
    }

    public void stopTask(View view) {
        MqttManager.INSTANCE.cancelTask(true);
    }
}
